﻿using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DullBoard.Model;

namespace DullBoard
{
    public partial class ctlPost : UserControl
    {
        private readonly Post _post;

        public ctlPost(Post post)
        {
            InitializeComponent();

            _post = post;
        }

        public void Load()
        {

            var sbDetail = new StringBuilder();
            sbDetail.AppendLine(_post.Author.Name);
            sbDetail.AppendLine("<" + _post.Author.Guild + ">");
            sbDetail.AppendLine(_post.Author.CharacterInfo);
            sbDetail.AppendLine(_post.Author.Realm);
            lblPostDetail.Text = sbDetail.ToString();

            RenderPost(_post.Detail);

        }

        private void RenderPost(IEnumerable<PostDetail> postDetails)
        {
            var cssStream = this.GetType().Assembly.GetManifestResourceStream("DullBoard.Resources.post.css");
            var sr = new StreamReader(cssStream);
            var css = sr.ReadToEnd();

            var text = new StringBuilder();
            var html = new StringBuilder();
            html.AppendLine("<html>");
            html.AppendLine("<head>");
            html.AppendLine("<style type=\"text/css\">");
            html.AppendLine(css);
            html.AppendLine("</style>");
            html.AppendLine("</head>");
            html.AppendLine("<body>");

            foreach(var post in postDetails)
            {
                if (post.IsQuote)
                {
                    html.AppendLine("<div class=\"quote-block\">");
                    html.AppendLine("<div>");
                    html.AppendLine("<span class=\"posted-by\">");
                    if (post.QuoteName != null)
                    {
                        html.AppendLine(string.Format("Posted by <span class=\"posted-by-user\">{0}</span> {1}",
                                                      post.QuoteName, post.QuoteDate));
                        text.AppendLine(string.Format("Posted by {0} {1}",
                                                      post.QuoteName, post.QuoteDate));
                    }
                    else
                    {
                        html.AppendLine("Quote");
                        text.AppendLine("Quote");
                    }
                    html.AppendLine("</span>");
                    html.AppendLine("</div>");
                    html.AppendLine(post.Text);
                    text.AppendLine(post.Text);
                    text.AppendLine();
                    html.AppendLine("</div>");
                    continue;
                   
                }

                if (post.IsLineBreak)
                {
                    html.AppendLine("<br/>");
                    text.AppendLine();
                    continue;
                }

                html.AppendLine(post.Text);
                text.AppendLine(post.Text);
            }

            html.AppendLine("</html>");
            html.AppendLine("</body>");

            wbPost.DocumentText = html.ToString();

            var aSize = TextRenderer.MeasureText(text.ToString(), this.Font, wbPost.Size,TextFormatFlags.WordBreak);
            
            if (aSize.Height > wbPost.Height)
                this.Height = aSize.Height + 40;

        }

    }
}
